package com.lumm.filter;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminLoginHandlerInterceptor implements HandlerInterceptor {

    private static final String[] IGNORE_URI = {
            "/admin","/admin/", "/admin/index.html","/admin/login"
    };

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //对请求路径进行判断
        String servletPath = httpServletRequest.getServletPath();
        boolean flag = false;   //用于存储判断登录的结果
        //判断请求是否需要拦截
        for (String s : IGNORE_URI) {
            if (servletPath.equals(s)) {
                flag = true;
                break;
            }
        }

        if (!flag) {   //如果是非公开的页面↓
            if (httpServletRequest.getSession().getAttribute("staffLogin") == null) {
                httpServletResponse.sendRedirect("/admin/index.html");
            } else {
                //用户登陆过，验证通过，放行
                flag = true;
            }
        }

        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}
